package com.example.service;

import com.example.bean.File;
import com.example.dao.FileDao;
import com.example.util.FileStreamUtil;
import com.example.util.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Path;
import java.nio.file.Paths;

@Service
public class FileService {
    private UuidUtil uuidUtil = new UuidUtil();
    private FileStreamUtil fileStreamUtil = new FileStreamUtil();
    @Autowired
    private FileDao fileDao;
    /**
     * 获取日志记录器
     */
    private Logger logger = LoggerFactory.getLogger(FileService.class);

    public Object uploadFile(MultipartFile file, String filepath){
        Path path = Paths.get(filepath + file.getOriginalFilename());
        logger.error("--------- "+path+" ------------");
        logger.error("--------- "+file.getOriginalFilename()+" ------------");
        File fileBean = new File();
        fileBean.setId(uuidUtil.getNewId());
        fileBean.setFilePath(path.toString());
        fileBean.setFileName(file.getOriginalFilename());
        fileDao.insertFile(fileBean);
        return fileStreamUtil.fileStream(file, filepath);

    }

}
